//
//  YYViewController.m
//  PinYinView
//
//  Created by “” on 02/14/2025.
//  Copyright (c) 2025 “”. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray         *chineseArray;
@property (nonatomic, strong) NSMutableArray         *pinYinArray;

@end

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    GetSafeAreaHeight
    _scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).mas_offset(0);
        make.top.mas_equalTo(self.view.mas_top).mas_offset(150);
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(kScreenHeight - SafeAreaHeight - NAV_HEIGHT);
    }];
    
    NSString *chineseWordFilePath = [[NSBundle mainBundle] pathForResource:@"心经" ofType:@"json"];
    NSLog(@"chineseWordFilePath = %@",chineseWordFilePath);
    NSData *wordInfoData = [NSData dataWithContentsOfFile:chineseWordFilePath];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:wordInfoData options:NSJSONReadingAllowFragments error:nil];
    NSArray *dataArr = dataDic[@"word"];
    for (NSInteger i = 0; i < dataArr.count; ++i) {
        NSString *wordString = dataArr[i];
        if ([wordString containsString:@" - "]) {
            NSArray *wordSubArr = [wordString componentsSeparatedByString:@" - "];
            NSString *wordString = [wordSubArr firstObject];
            NSString *pinYinString = [wordSubArr lastObject];
            NSArray *wordArr = [NSString slicingString:wordString];
            NSArray *pinYinArr = [pinYinString componentsSeparatedByString:@" "];
            [self.chineseArray addObjectsFromArray:wordArr];
            [self.pinYinArray addObjectsFromArray:pinYinArr];
            if (i == dataArr.count - 1) {
                
            } else {
                [self.chineseArray addObject:@"\n"];
                [self.pinYinArray addObject:@""];
            }
        }
        
        
    }
    

    PinYinConfig *config = [PinYinConfig new];
    config.pinYinColor = [UIColor magentaColor];
    config.wordColor = [UIColor purpleColor];
    config.pinYinFont = [UIFont systemFontOfSize:15];
    config.wordFont = [UIFont systemFontOfSize:20];
//    config.viewSize = [NSValue valueWithCGSize:CGSizeMake(60, 80)];
    PinYinView *textView = [PinYinView createTextViewWidth:kScreenWidth - 40 contentArray:self.chineseArray pinYinArray:self.pinYinArray config:config];
    [_scrollView addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.top.mas_equalTo(_scrollView.mas_top).mas_offset(0);
        make.bottom.mas_lessThanOrEqualTo(_scrollView.mas_bottom).mas_offset(0);
    }];
}



- (NSMutableArray *)chineseArray {
    if (!_chineseArray) {
        _chineseArray = [NSMutableArray new];
    }
    return _chineseArray;
}

- (NSMutableArray *)pinYinArray {
    if (!_pinYinArray) {
        _pinYinArray = [NSMutableArray new];
    }
    return _pinYinArray;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
